package cn.book.org.controller;

import cn.book.org.service.IClassesService;
import cn.book.org.domain.Classes;
import cn.book.org.query.ClassesQuery;
import cn.book.basic.utils.AjaxResult;
import cn.book.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    public IClassesService classesService;


    /**
     * 保存和修改公用的
     * @param classes  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Classes classes){
        try {
            if( classes.getId()!=null)
                classesService.update(classes);
            else
                classesService.save(classes);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            classesService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Classes get(@PathVariable("id")Long id)
    {
        return classesService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Classes> list(){

        return classesService.loadAll();
    }


    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/7/3 21:14
    * @Parameters: [ids]
    * @Return void
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids) {
        try {
            classesService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public PageList<Classes> json(@RequestBody ClassesQuery query)
    {
        return classesService.queryData(query);
    }

}
